#include"stackqueue.h"
typedef char StackDataType;

typedef struct StackNode
{
    StackDataType* _stack;
    int _capacity;
    int _top;
}Stack;

void StackInit(Stack* st);
void StackPush(Stack* st, StackDataType data);
void StackPop(Stack* st);
StackDataType StcakTop(Stack* st);
int StackSize(Stack* st);
int StackEmpty(Stack* st);
void StackDestory(Stack* st);
void StackInit(Stack* st)
{
    st->_stack = NULL;
    st->_capacity = 0;
    st->_top = 0;
}
void StackPush(Stack* st, StackDataType data)
{
    assert(st);
    if (st->_capacity == st->_top)
    {
        int newCapacity = st->_capacity == 0 ? 4 : st->_capacity * 2;
        st->_stack = (StackDataType*)realloc(st->_stack, newCapacity * sizeof(StackDataType));
        st->_capacity = newCapacity;
    }
    st->_stack[st->_top] = data;
    st->_top++;
}
void StackPop(Stack* st)
{
    assert(st);
    assert(st->_top);
    st->_top--;
}
StackDataType StcakTop(Stack* st)
{
    assert(st);
    return st->_stack[st->_top - 1];
}
int StackSize(Stack* st)
{
    assert(st);
    return st->_top;
}
int StackEmpty(Stack* st)
{
    assert(st);
    if (st->_top == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void StackDestory(Stack* st)
{
    assert(st);
    free(st->_stack);
    st->_stack = NULL;
    st->_capacity = st->_top = 0;

}
bool isValid(char* s) {
    Stack st;
    StackInit(&st);
    int count = 0;
    while (s[count])
    {
        if (s[count] == '('
            || s[count] == '['
            || s[count] == '{')
        {
            StackPush(&st, s[count]);
        }
        else
        {
            if (StackEmpty(&st))
            {
                StackDestory(&st);
                return false;
            }
            char top = StcakTop(&st);
            if (top == '(' && s[count] != ')'
                || top == '{' && s[count] != '}'
                || top == '[' && s[count] != ']')
            {
                StackDestory(&st);
                return false;
            }
            StackPop(&st);
        }
        count++;
    }
    int  result = StackEmpty(&st);
    StackDestory(&st);
    return result;
}